layui.use(['form','element','table','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;

    //获取路径参数
    var request = GetRequest();

    $.get('/produce/trace/list?page=0&limit=10&id=' + request.traceNumber,function (data) {
        if (data.data.length != 0) {
            var traceInfo = data.data[0];
            //默认加载产品基本信息
            $.get("/produce/" + traceInfo.produceId,function (data) {
                $("#loading").hide();
                //产品图片
                $("#pic").val(data.pic);
                var picHtml = '';
                if (data.pic != null && data.pic != "") {
                    var picArr = data.pic.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        picHtml += '<img style="padding: 5px;border: solid aliceblue;" src="'+ picArr[i] +'" class="layui-upload-img" width="30%">';
                    }
                }
                //采用标准
                var adoptStandardHtml = '';
                if (data.adoptStandard != null && data.adoptStandard != "") {
                    var picArr = data.adoptStandard.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        adoptStandardHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //质检报告
                var qualityInspectionReportHtml = '';
                if (data.qualityInspectionReport != null && data.qualityInspectionReport != "") {
                    var picArr = data.qualityInspectionReport.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        qualityInspectionReportHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //质量管理体系认证书
                var certificateOfQualityHtml = '';
                if (data.certificateOfQuality != null && data.certificateOfQuality != "") {
                    var picArr = data.certificateOfQuality.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        certificateOfQualityHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //产品许可证
                var productLicenseHtml = '';
                if (data.productLicense != null && data.productLicense != "") {
                    var picArr = data.productLicense.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        productLicenseHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                var innerHtml = '<form class="layui-form">' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产品名称：</label>' +
                    '                <div class="layui-input-inline" id="name">'+ data.name +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产品类型：</label>' +
                    '                <div class="layui-input-block">产品类型</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品图片：</label>' +
                    '                <div class="layui-input-block">' + picHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-row layui-col-xs12">' +
                    '                <label class="layui-form-label">单价：</label>' +
                    '                <div class="layui-input-inline">'+ data.price +'元/斤</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">来源企业：</label>' +
                    '                <div class="layui-input-block">'+ traceInfo.companeName +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品简介：</label>' +
                    '                <div class="layui-input-block">'+ data.proDescribe +'</div>' +
                    '            </div>' +
                    '        </form>' +
                    '        <blockquote id="produceQuality" style="margin-top: 50px;" class="layui-elem-quote">' +
                    '            产品资质' +
                    '        </blockquote>'+
                    '        <form class="layui-form">'+
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产地信息：</label>' +
                    '                <div class="layui-input-block" >'+ data.originInformation +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">采用标准：</label>' +
                    '                <div class="layui-input-block">' + adoptStandardHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">质检报告：</label>' +
                    '                <div class="layui-input-block">' + qualityInspectionReportHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">质量管理体系认证书：</label>' +
                    '                <div class="layui-input-block">' + certificateOfQualityHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品许可证：</label>' +
                    '                <div class="layui-input-block">' + productLicenseHtml +
                    '                </div>' +
                    '            </div>' +
                    '        </form>';
                $(".right-content").append(innerHtml);
            });

            //刷新左侧生产过程流程
            $.get("/process/tree/" + traceInfo.processTemplateId,function (data) {
                $.each(data.data,function (i, ele) {
                    var lihtml = '<li class="process-li"> <a href="javascript:;" data-table="'+ ele.tableType +'" data-probatch="'+ traceInfo.id +'" data-template="'+ ele.id + '" class="ztb_con_text" >'+ ele.name +'</a>';

                    //如果存在二级
                    if (ele.children != null && ele.children.length != 0) {
                        lihtml = '<li class="process-li"> <a href="javascript:;" class="ztb_con_text" onclick="clickli(this);">生产信息</a>' +
                            '         <ul class="ztb_content_02">';
                        $.each(ele.children,function (j, ele) {
                            lihtml += '<li class="process-li"><a href="javascript:;" data-table="'+ ele.tableType +'" data-probatch="'+ traceInfo.id +'" data-template="'+ ele.id + '" >'+ (j + 1) +'.'+ ele.name +'</a></li>';
                        });
                    }
                    lihtml += '</ul></li>';
                    $("#trace-nav").append(lihtml);
                });

                //绑定生产过程事件
                $(".process-li a").click(function () {
                    var table = $(this).data('table');
                    var probatch = $(this).data('probatch');
                    var template = $(this).data('template');

                    //无二级li标签
                    if (table == null) {
                        return;
                    }
                    //添加
                    $(".right-content").html('');
                    var innerHtml = '<blockquote style="margin-top: 5px;border: 0 none;" class="layui-elem-quote">' +
                        '            ' + $(this).html() +
                        '        </blockquote>' +
                        '        <div class="layui-tab">' +
                        '            <ul class="layui-tab-title" id="table-list" >' +
                        '            </ul>' +
                        '            <div class="layui-tab-content" id="table-content" >' +
                        '            </div>' +
                        '        </div>';
                    $(".right-content").append(innerHtml);
                    var tableArr = table.split(",");
                    $.each(tableArr,function (index, table) {
                        if (index === 0) {
                            var contentHtml = '';
                            if (table == 'invest') {
                                $("#table-list").append('<li class="layui-this">投入品记录</li>');
                                contentHtml = '<div class="layui-tab-item layui-show">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'check') {
                                $("#table-list").append('<li class="layui-this">检查记录</li>');
                                contentHtml = '<div class="layui-tab-item layui-show">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'machining') {
                                $("#table-list").append('<li class="layui-this">加工记录</li>');
                                contentHtml = '<div class="layui-tab-item layui-show">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'transport') {
                                $("#table-list").append('<li class="layui-this">运输记录</li>');
                                contentHtml = '<div class="layui-tab-item layui-show">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else {
                                $.ajax({
                                    url: '/table/' + table,
                                    type: 'get',
                                    async: false,
                                    success: function (data) {
                                        $("#table-list").append('<li class="">'+ data.nameZh +'</li>');
                                    }
                                });
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/tracerecord?tableName='+ table +'&id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            }
                            $("#table-content").append(contentHtml);
                        } else {
                            var contentHtml = '';
                            if (table == 'invest') {
                                $("#table-list").append('<li class="">投入品记录</li>');
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'check') {
                                $("#table-list").append('<li class="">检查记录</li>');
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'machining') {
                                $("#table-list").append('<li class="">加工记录</li>');
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else if (table == 'transport') {
                                $("#table-list").append('<li class="">运输记录</li>');
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/'+ table +'?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            } else {
                                $.ajax({
                                    url: '/table/' + table,
                                    type: 'get',
                                    async: false,
                                    success: function (data) {
                                        $("#table-list").append('<li class="">'+ data.nameZh +'</li>');
                                    }
                                });
                                contentHtml = '<div class="layui-tab-item">' +
                                    '                <iframe id="colChanelList" src="/traceability/tracerecord?tableName='+ table +'&id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                                    '</div>';
                            }
                            $("#table-content").append(contentHtml);
                        }
                    });

                    //添加默认环境监控设备设备
                    $("#table-list").append('<li class="">产品环境数据</li>');
                    var contentHtml = '<div class="layui-tab-item">' +
                        '                <iframe src="/traceability/tracerecordMonitor?id=' + probatch + '&processId='+ template +'" frameborder="0"></iframe>' +
                        '</div>';
                    $("#table-content").append(contentHtml);

                    //点击tab导航刷新表格
                    $("#table-list li").click(function () {
                        var iframe = $("#table-content .layui-tab-item").eq($(this).index()).find('iframe');
                        var url = iframe.attr("src");
                        iframe.attr("src",url);
                    });
                });

                //当前阶段显示
                $('ul.ztb_content_02 li').click(function(){
                    $('ul.ztb_content_02 li').removeClass("ztb_active");
                    $(this).addClass("ztb_active");
                    $(this).addClass('ztb_online').siblings('li.ztb_online').removeClass('ztb_online');
                    $(this).parents('li').siblings('li').children('ul').find('li.ztb_online').removeClass('ztb_online');
                })
            });
        } else {
            $(".right-content").html('<p style="margin-top: 200px;font-size: 16px;">未找到数据，请确定溯源码是否正确？</p>');
        }
        
        //点击展示产品信息
        $("#produceInfo li").click(function () {
            $("#loading").show();
            $(".right-content").html('');
            $.get("/produce/" + traceInfo.produceId,function (data) {
                $("#loading").hide();
                //产品图片
                $("#pic").val(data.pic);
                var picHtml = '';
                if (data.pic != null && data.pic != "") {
                    var picArr = data.pic.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        picHtml += '<img style="padding: 5px;border: solid aliceblue;" src="'+ picArr[i] +'" class="layui-upload-img" width="30%">';
                    }
                }
                //采用标准
                var adoptStandardHtml = '';
                if (data.adoptStandard != null && data.adoptStandard != "") {
                    var picArr = data.adoptStandard.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        adoptStandardHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //质检报告
                var qualityInspectionReportHtml = '';
                if (data.qualityInspectionReport != null && data.qualityInspectionReport != "") {
                    var picArr = data.qualityInspectionReport.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        qualityInspectionReportHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //质量管理体系认证书
                var certificateOfQualityHtml = '';
                if (data.certificateOfQuality != null && data.certificateOfQuality != "") {
                    var picArr = data.certificateOfQuality.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        certificateOfQualityHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //产品许可证
                var productLicenseHtml = '';
                if (data.productLicense != null && data.productLicense != "") {
                    var picArr = data.productLicense.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        productLicenseHtml += '<img style="padding: 5px;border: solid aliceblue;" width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                var innerHtml = '<blockquote style="margin-top: 5px;" class="layui-elem-quote">' +
                    '               基本信息' +
                    '            </blockquote>' +
                    '            <form class="layui-form">' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产品名称：</label>' +
                    '                <div class="layui-input-inline" id="name">'+ data.name +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产品类型：</label>' +
                    '                <div class="layui-input-block">产品类型</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品图片：</label>' +
                    '                <div class="layui-input-block">' + picHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-row layui-col-xs12">' +
                    '                <label class="layui-form-label">单价：</label>' +
                    '                <div class="layui-input-inline">'+ data.price +'元/斤</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">来源企业：</label>' +
                    '                <div class="layui-input-block">'+ traceInfo.companeName +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品简介：</label>' +
                    '                <div class="layui-input-block">'+ data.proDescribe +'</div>' +
                    '            </div>' +
                    '        </form>' +
                    '        <blockquote id="produceQuality" style="margin-top: 50px;" class="layui-elem-quote">' +
                    '            产品资质' +
                    '        </blockquote>'+
                    '        <form class="layui-form">'+
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">产地信息：</label>' +
                    '                <div class="layui-input-block" >'+ data.originInformation +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">采用标准：</label>' +
                    '                <div class="layui-input-block">' + adoptStandardHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">质检报告：</label>' +
                    '                <div class="layui-input-block">' + qualityInspectionReportHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">质量管理体系认证书：</label>' +
                    '                <div class="layui-input-block">' + certificateOfQualityHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">产品许可证：</label>' +
                    '                <div class="layui-input-block">' + productLicenseHtml +
                    '                </div>' +
                    '            </div>' +
                    '        </form>';
                $(".right-content").append(innerHtml);
            });
        });
        
        //点击展示生产商信息
        $("#zb li").click(function () {
            $("#loading").show();
            $(".right-content").html('');
            $.get("/company/" + traceInfo.manufactorId,function (data) {
                $("#loading").hide();
                //法人资格证书
                var qualificationCertificateHtml = '';
                if (data.qualificationCertificate != null && data.qualificationCertificate != "") {
                    qualificationCertificateHtml += '<img style="padding: 5px;border: solid aliceblue; " width="20%" src="'+ data.qualificationCertificate +'" class="layui-upload-img">';
                }
                //企业荣誉
                var honorsHtml = '';
                if (data.honors != null && data.honors != "") {
                    var picArr = data.honors.split(";");
                    for (var i = 0;i < picArr.length -1 ;i++) {
                        honorsHtml += '<img style="padding: 5px;border: solid aliceblue; " width="20%" src="'+ picArr[i] +'" class="layui-upload-img">';
                    }
                }
                //企业介绍
                var innerHtml = '<blockquote style="margin-top: 5px;" class="layui-elem-quote">' +
                    '               基本信息' +
                    '            </blockquote>' +
                    '            <form class="layui-form">' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">企业名称：</label>' +
                    '                <div class="layui-input-inline" id="name">'+ data.companeName +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">电话号码：</label>' +
                    '                <div class="layui-input-block">'+ data.tel +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">电子邮箱：</label>' +
                    '                <div class="layui-input-block">' + data.email +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-row layui-col-xs12">' +
                    '                <label class="layui-form-label">详细地址：</label>' +
                    '                <div class="layui-input-block">'+ data.address +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">网站首页：</label>' +
                    '                <div class="layui-input-block">'+ data.webAddress +'</div>' +
                    '            </div>' +
                    '        </form>' +
                    '        <blockquote id="produceQuality" style="margin-top: 50px;" class="layui-elem-quote">' +
                    '            公司资质' +
                    '        </blockquote>'+
                    '        <form class="layui-form">'+
                    '            <div class="layui-form-item">' +
                    '                <label class="layui-form-label">执照注册码：</label>' +
                    '                <div class="layui-input-block" >'+ data.blRegiserNo +'</div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">执照所在地：</label>' +
                    '                <div class="layui-input-block">' + data.blAddress +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">营业期限：</label>' +
                    '                <div class="layui-input-block">' + data.blDate +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">营业范围：</label>' +
                    '                <div class="layui-input-block">' + data.blCourse +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">组织机构代码：</label>' +
                    '                <div class="layui-input-block">' + data.organizationalCode +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">法人资格证书：</label>' +
                    '                <div class="layui-input-block">'+ qualificationCertificateHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <div class="layui-form-item layui-form-text">' +
                    '                <label class="layui-form-label">企业荣誉：</label>' +
                    '                <div class="layui-input-block">'+ honorsHtml +
                    '                </div>' +
                    '            </div>' +
                    '            <blockquote id="produceQuality" style="margin-top: 50px;" class="layui-elem-quote">' +
                    '               公司介绍' +
                    '            </blockquote>'+
                    '            <form class="layui-form">'+
                    '            <div class="layui-form-item">' +
                    '                <div class="layui-input-block" >'+ data.introduce +'</div>' +
                    '            </div>' +
                    '        </form>';
                $(".right-content").append(innerHtml);
            });
        });
    });



    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});
